/*
 * Copyright 2011 OpenWAF.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.openwaf.client.ui.basic;

import com.openwaf.client.dom.Element;
import com.openwaf.client.dom.InputElement;
import com.openwaf.client.event.dom.ClickEvent;
import com.openwaf.client.event.dom.ClickHandler;
import com.openwaf.client.ui.annotation.ViewMethod;
import com.openwaf.client.ui.annotation.ViewProperty;

public final class Checkbox extends BasicControl {

    private InputElement elmChk;
    private InputElement elmLbl;
    private static String  CSS_NORMAL="waf-basic-checkbox";
    private static String  CSS_DISABLED="waf-basic-checkbox-disabled";

    public Checkbox() {
        setText("Button");
        elmLbl.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                __clickInput(elmChk);
            }
        });       
    }
    @ViewProperty
    public void setDisabled(boolean value){
        if(value){
            getElement().setCssClassName(CSS_DISABLED);
        }else{
            getElement().setCssClassName(CSS_NORMAL);
        }
        elmChk.setDisabled(value);
    }
    @ViewProperty
    public void setDisabled(String value){
        setDisabled(Boolean.parseBoolean(value));
    }

    public Checkbox(String text) {
        this();
        setText(text);
    }

    public InputElement getCheckboxElement() {
        return elmChk;
    }

    public Element getLabelElement() {
        return elmLbl;
    }

    @ViewProperty
    public void setText(String text) {
        elmLbl.setInnerHTML(text);
    }

    @ViewMethod
    public void setText(Element element){
        elmLbl.clearChilds();
        elmLbl.appendChild(element);
    }

    public String getText() {
        return elmLbl.getInnerHTML();
    }

    @ViewProperty
    public void setChecked(boolean v) {
        elmChk.setChecked(v);
    }

    public boolean getChecked() {
        return elmChk.isChecked();
    }

    private native void __clickInput(InputElement e)/*-{
    if(e.disabled==false){
    e.checked=!e.checked;
    }
    }-*/;
}
